package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.activitys.ChangePassActivity;
import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.UserBean;
import com.wantong.jianpu.models.OtherModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.Constants;
import com.wantong.jianpu.utils.XUtils;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;

/**
 * @author PACY
 * @date 2022/4/24
 * description:
 */
public class MineViewModel extends BaseViewModel {
    public OtherModel model;
    public SingleLiveEvent<UserBean> userInfo=new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> exitLogin=new SingleLiveEvent<>();
    public MineViewModel(@NonNull Application application) {
        super(application);
        model=new OtherModel();
    }

    @Override
    protected void init() {
        super.init();
        if(XUtils.isSaveInfo()){
            getUserInfo();
        }else{
            UserBean userBean=new UserBean();
            userBean.setUserName(XUtils.getKey(Constants.USERNAME));
            userBean.setNickName(XUtils.getKey(Constants.NICKNAME));
            userBean.setPhonenumber(XUtils.getKey(Constants.PHONE_NUMBER));
            userBean.setDept(new UserBean.DeptDTO());
            userBean.getDept().setDeptName(XUtils.getKey(Constants.DEPT_NAME));
            userInfo.setValue(userBean);
        }
    }

    /**
     * 获取个人信息
     */
    public void getUserInfo(){
        model.getUserInfo(new BaseObserver<UserBean>() {
            @Override
            public void onSuccess(BaseResultBean<UserBean> t) {
                XUtils.saveUser(t.getData());
                userInfo.setValue(t.getData());
            }
        },getLifecycleProvider());
    }

    /**
     * 修改密码
     */
    public BindingCommand toChangePassPage=new BindingCommand(() -> {
      startActivity(ChangePassActivity.class);
    });

    /**
     * 退出登录
     */
    public BindingCommand exit=new BindingCommand(() -> {
        exitLogin.setValue(true);
    });
}
